﻿//#define MESS
#define INFO
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using Gqq.Common.Helper.CodeFirstLog;



namespace Gqq.CodeFirst.ProcessPalace
{

    public class PalaceCommon : PartBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentProperties"></param>
        /// <param name="properties">DbPropertyValues中有索引器。可以通过index或者属性名进行选择
        /// PropertyNames属性包括所有的名
        /// </param>
        public static void GetAllProperties(string parentProperties, DbPropertyValues properties)
        {
            foreach (var pName in properties.PropertyNames)
            {
                CodeLogManager.DLog.DebugFormat("pName:{0}", pName);
                var property = properties[pName] as DbPropertyValues;
                if (property != null)
                {
                    GetAllProperties(parentProperties + pName + ".", property);
                }
                else
                {
                    Console.WriteLine("{0}.{1} has value {2}", parentProperties, pName, properties[pName]);
                }
            }
        }

        public static void TestIf()
        {
#if DEBUG
            Console.WriteLine("It is Debugging");
#elif INFO
           Console.WriteLine("It is INFOING");
#else
           Console.WriteLine("It is Releasing");
#endif
        }

        /// <summary>
        /// 获得最大的princess Id
        /// </summary>
        /// <returns></returns>
        public static int GetMaxPrincessId()
        {
            var princess = db.Princesses.OrderByDescending(x => x.Id).FirstOrDefault();
            if (princess == null) return 0;
            return princess.Id;
        }

        /// <summary>
        /// 测试如何直接进行数据库的操作。
        /// 如果先进行读取操作，然后更新数据库，这样的话，以前读取的数据是不变的。
        /// </summary>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public static string TestDatabaseValue(string oldName,string newName)
        {
            var id = 27;
            var sql = string.Format("update princess set name='{0}' where id={1}", oldName, id);
            db.Database.ExecuteSqlCommand(sql);
            var princess = db.Princesses.Find(id);
            sql = string.Format("update princess set name='{0}' where id={1}", newName, id);
            db.Database.ExecuteSqlCommand(sql);
            string name = db.Entry(princess).GetDatabaseValues().GetValue<string>("Name");
            return name + "_" + princess.Name;
        }
    }
}
